import { ConfigModule_blxf } from "./ConfigModule";


const {ccclass, property} = cc._decorator;

/** 界面整体适配 */
@ccclass
export class AdapterModule_blxf extends cc.Component {

    @property({ type: cc.Canvas, tooltip: 'Canvas画布'})
    Canvas: cc.Canvas = null;


    // onLoad () {}

    start () {
        // this.scheduleOnce(()=>{
            ConfigModule_blxf.instance.initWinSize_blxf();
        // })

        this.onAdaptation_blxf();
    }

    // update (dt) {}

    onAdaptation_blxf () {
        let winSize = cc.winSize;
        let { gameW, gameH, scaleRatio } = ConfigModule_blxf.instance;
        let scaleWidth = winSize.width / gameW;
        let scaleHeight = winSize.height / gameH;
        let _scale = Math.min(scaleWidth, scaleHeight);
        
        if ( !scaleRatio ) {
            let _isIpad: boolean = false;
            let _isScaleWidth = scaleWidth <= scaleHeight;
            if ( _scale < 1 ) {
                _scale = 1;
                _isIpad = true;
            }
            ConfigModule_blxf.instance.setAdapterScaleData_blxf( _scale, _isScaleWidth, _isIpad );
            cc.log('_isScaleWidth', _isScaleWidth, _scale, scaleWidth, scaleHeight, winSize.width, winSize.height);
        }
        this.node.scale = ConfigModule_blxf.instance.scaleRatio;
        
        this.onAdaptationCanvas_blxf(winSize);
    };

    onAdaptationCanvas_blxf (winSize) {
        if ( !this.Canvas ) return;
        let designSize = this.Canvas['_designResolution'];
        let screenRatio = winSize.width / winSize.height;
        let designRatio = designSize.width / designSize.height;
        
        if (screenRatio > 1) return this.setFit_blxf(false);
        this.setFit_blxf( screenRatio <= designRatio );
    };

    setFit_blxf ( isWidth ) {
        this.Canvas.fitWidth = isWidth;
        this.Canvas.fitHeight = !isWidth;
    };

}
